/*
 *@description:发布资讯
 *@Author:@reynold
 *@Date:2025-03-22 11:08:38
 *@Update:2025-03-22 11:08:38
 *@Version: V1.0.0
*/

<template>
  <Card>
    <div class="publish-container">
      <h1 class="page-title">发布家政服务资讯</h1>
      <el-form :model="formData" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="资讯标题" prop="title">
          <el-input v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="formData.author"></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker
            v-model="formData.publishTime"
            type="datetime"
            placeholder="选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资讯内容" prop="content">
          <el-input
            v-model="formData.content"
            type="textarea"
            :rows="4">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm">重置</el-button>
          <el-button type="primary" @click="submitForm">发布</el-button>
        </el-form-item>
      </el-form>
    </div>
  </Card>
</template>

<script>
import Card from '@/components/Card/index.vue'
import instance from '@/utils/instance.js'

export default {
  data() {
    return {
      formData: {
        title: '',
        author: '',
        publishTime: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入资讯标题', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请输入作者', trigger: 'blur' }
        ],
        publishTime: [
          { required: true, message: '请选择发布时间', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入资讯内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 模拟将数据添加到列表中，实际使用时应调用后端接口
          instance.post('/inforMsg/add', this.formData).then(res => {
            if (res.data.status === 200) {
              this.$message.success('资讯发布成功');
              this.resetForm();
            } else {
              this.$message.error('资讯发布失败');
            }
          }).catch(error => {
            console.error('发布资讯失败:', error);
            this.$message.error('发布资讯时出现错误');
          });
        } else {
          this.$message.warning('请填写完整资讯信息');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.formRef.resetFields();
    }
  },
  components: {
    Card
  }
}
</script>

<style lang="less" scoped>
.publish-container {
  padding: 20px;
}

.page-title {
  font-size: 24px;
  margin-bottom: 20px;
}
</style>    